﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Xsl;
using SpCop.Model;

namespace SpCop
{
    internal class ReportWriter
    {
        private static readonly Type ReportType = typeof (Report);
        private static readonly XmlSerializer Serializer = new XmlSerializer(ReportType);
        private static readonly XslCompiledTransform Transform;

        static ReportWriter()
        {
            Transform = new XslCompiledTransform();
            using (Stream styleSheetStream = typeof(ReportWriter).Assembly.GetManifestResourceStream("SpCop.Report.xslt"))
            {
                if (styleSheetStream == null) return;

                using (XmlReader styleSheetReader = XmlReader.Create(styleSheetStream))
                {
                    Transform.Load(styleSheetReader);
                }
            }
        }

        public static void WriteToXmlFile(Report report, string outputFile)
        {
            using (XmlWriter writer = XmlWriter.Create(outputFile))
            {
                Serializer.Serialize(writer, report);
            }
        }

        public static void ConvertToHtmlFile(string inputFile, string outputFile)
        {
            Transform.Transform(inputFile, outputFile);
        }
    }
}